<?php
//框架核心文件
/*
作者：好还啦
QQ：985753056
email:985753056@qq.com
 */
define('APP_INCLUDE',APP_ROOT . 'include/');//定义配置文件根目录常量


class Ok{
	public $config = '';
	public $route = '';
	public $url = '';


	//开始运行
	public function run(){
		$list = $this->_file();
		if(!is_file($list['file'])){
			echo '<br><b>Fatal error</b> :file <b>app/' . $list['m'] .'/controller ' . $list['c'] . '.php </b>Not  found';
			return;
		}
		require($list['file']);
		$obj = 'app\\'. $list['m'] .'\controller\\' . $list['c'];
		$obj = new $obj();
		//请求的方法 不存在
		if(!method_exists($obj,$list['f'])){
			echo '<br><b>Fatal error</b> :f <b>app/' . $list['m'] . '/' . $list['c'] .'/'. $list['f'] . '</b> Not found';
			return ;
		}
		$rs = $obj->{$list['f']}();
		if(is_string($rs) || is_numeric($rs)){
			echo $rs;
		}
	}

	/**
	 * @return [str] [根据请求地址获取 url] 
	 */
	private function _getUrl(){
		$url = rtrim(str_replace('index.php','',str_replace(str_replace($_SERVER['DOCUMENT_ROOT'],'',str_replace('\\','/',APP_ROOT)),'',$_SERVER['REQUEST_URI'])),'/');
		if(stripos($url,'?') === false){
			return $this->url = $url;
		}
		$arr = explode('?',$url);
		$this->url = rtrim($arr[0],'/'). '/' .str_replace('&','/',str_replace('=','/',$arr[1]));
	}

	/**
	 * @return [array] [拆分 url] 
	 */
	private function _splitUrl(){
		if(!$this->url){
			return [];
		}
		return explode('/',$this->url);
	}

	/**
	 * @return [str] [补全 url] 
	 */
	private function _buqi(){
		$url = $this->url;
		$urlArrLen = count($this->_splitUrl());
		if($urlArrLen == 0){
			return 'index/index/index';
		}elseif($urlArrLen == 1){
			return $url . '/index/index';
		}elseif($urlArrLen == 2){
			return $url . '/index';
		}else{
			return $url;
		}
	}

	/**
	 * 匹配路由
	 * 匹配到返回路由地址  没有匹配到返回空
	 * @return [str]
	 */
	private function _chechRoute(){
		$url = $this->url;
		$urlArrLen = count($this->_splitUrl());
		while($urlArrLen){
			if(array_key_exists($url,$this->route)){
				break;
			}
			$arr = explode('/',$url);
			array_pop($arr);
			$urlArrLen = count($arr);
			$url = implode($arr,'/');
		}
		if($url){
			return $url;
		}
		return '';
	}

	/**
	 * 分析 url
	 * 返回 由 模块/控制器/方法 和 参数 组成的二维数组
	 * 分三种情况分析
	 * 0 ，不使用路由
	 * 1 ，必须使用路由
	 * 2 ，两者均可
	 * @return [array] 
	 */
	private function _analyseUrl(){
		if($this->config['route'] == 0){//0 ，不使用路由
			$url = $this->_buqi();
		}elseif($this->config['route'] == 1){//1 ，必须使用路由
			$route = $this->_chechRoute();
			if(!$route){
				echo  '<br><b>Fatal error</b> :没有配置路由，或者将 ifconfig.php 文件的路由设置 成 0 或者 2';
				return ;
			}
			$url = str_replace($route,$this->route[$route],$this->url);
		}elseif($this->config['route'] == 2){//2 ，两者均可
			$route = $this->_chechRoute();
			if($route){
				$url = str_replace($route,$this->route[$route],$this->url);
			}else{
				$url = $this->_buqi();
			}
		}
		$arr = explode('/',$url);
		$value = array_splice($arr,3);
		return ['url' => rtrim(str_replace(implode($value,'/'),'',$url),'/'),'value' => $value];
	}

	/**
	 * @return [str] [获取 模块/控制器/方法 部分]
	 */
	private function _mcf(){
		return $this->_analyseUrl()['url'];
	}

	/**
	 * @return [array] [获取 参数 部分]
	 */
	private function _value(){
		return $this->_analyseUrl()['value'];
	}

	/**
	 * @return [str] [获取 当前请求的模块]
	 */
	private function _modular(){
		return explode('/',$this->_mcf())[0];
	}

	/**
	 * @return [array] [获取请求的文件路径]
	 */
	private function _file(){
		$arr = explode('/',$this->_mcf());
		$list['m'] = $arr[0];
		$list['c'] = $arr[1];
		$list['f'] = $arr[2];
		$list['file'] = APP_PATH . $arr[0] . '/' . 'controller/' . $arr[1] . '.php';
		return $list;
	}

	/**
	 * @return [array] [获取系统配置信息 config.php]
	 */
	private function _getCfg(){
		require(APP_INCLUDE . 'config/config.php');
		$this->config = $cfg;
	}

	/**
	 * @return [array] [获取路由配置信息 route.php]
	 */
	private function _getRoute(){
		require(APP_INCLUDE . 'config/route.php');
		$this->route = $route;
	}

	//自动加载
	private function _load(){
		spl_autoload_register(function ($class){
			$arr = explode('\\',$class);
			if($arr[0] == 'ok'){
				require(str_replace('\\','/',APP_ROOT . 'okphp/library/' . $class . '.php'));
			}else{
				require(str_replace('\\','/',APP_ROOT . $class . '.php'));
			}
		});
	}

	//加载 助手函数文件
	private function _loadFunc(){
		global $value,$modular;
		$value = $this->_value();
		$modular = $this->_modular();
		require(APP_INCLUDE . 'function.php');		
	}


	public function __construct(){
		$this->_getCfg();
		$this->_getRoute();
		$this->_getUrl();
		$this->_load();
		$this->_loadFunc();
	}
}